using System.Collections.Generic;
using System.Xml;

namespace ServiceModel.Syndication.LEAP2A {
    /// <summary>
    /// Used to represent time information connected with the subject matter of a record
    /// </summary>
    public class LeapSpatial {
        /// <summary>
        /// Gets or sets an unstructured textual version of the address.
        /// </summary>
        /// <value>The value.</value>
        public string Value {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a structured version of the address.
        /// </summary>
        /// <value>The address.</value>
        public LeapSpatialAddress Address {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapDate"/> class.
        /// </summary>
        public LeapSpatial() {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapSpatial"/> class.
        /// </summary>
        /// <param name="value">The country name.</param>
        public LeapSpatial(string value) {
            this.Value = value;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LeapSpatial"/> class.
        /// </summary>
        /// <param name="address">A structured address.</param>
        public LeapSpatial(LeapSpatialAddress address) {
            this.Address = address;
        }

        /// <summary>
        /// Tries the parse leap spatial element.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public virtual bool TryParseLeapSpatial(XmlReader reader) {
            if (reader.LocalName == "spatial") {
                if (!reader.IsEmptyElement) {
                    reader.Read();
                    
                    if (reader.NodeType == XmlNodeType.Text){
                        string address = reader.Value;
                        this.SplitAddress(reader.Value);
                    } else if (reader.NamespaceURI == "http://www.w3.org/1999/xhtml") {
                        reader.Read();
                        this.SplitAddress(reader.Value);
                        reader.Read();
                        reader.Read();
                    } else {
                        this.Address = new LeapSpatialAddress();
                        this.Address.TryParseLeapSpatial(reader);
                    }

                    return true;
                }
            }

            return false;
        }

        private void SplitAddress(string address) {
            if (address != null){
                string[] split = address.Split('\n');

                for (int i = 0; i < split.Length; i++){
                    string newLine = split[i].Trim('\r', '\t', ' ');

                    if (!string.IsNullOrEmpty(newLine)){
                        this.Value += newLine + "\r\n";
                    }
                }

                this.Value = this.Value.TrimEnd('\r', '\n');
            }
        }

        protected void WriteAddress(XmlWriter writer, LeapFeed feed) {
            if (Address == null) {
                writer.WriteElementString("div", "http://www.w3.org/1999/xhtml", Value);
            } else {
                Address.WriteTo(writer, feed);
            }
        }

        /// <summary>
        /// Writes to a LEAP2A feed.
        /// </summary>
        /// <param name="writer">The writer.</param>
        public virtual void WriteTo(XmlWriter writer, LeapFeed feed) {
            writer.WriteStartElement(
                feed.LeapPredicatesNamespacePrefix,
                "spatial",
                feed.LeapPredicatesNamespace);

            WriteAddress(writer, feed);

            writer.WriteEndElement();
        }
    }
}
